#include "adc.h"

void adc_init(void)
{
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);

	GPIO_InitTypeDef GPIO_init;
	GPIO_init.GPIO_Mode =   GPIO_Mode_AN;
	GPIO_init.GPIO_Pin  =		GPIO_Pin_0;
	GPIO_init.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_init.GPIO_PuPd	= GPIO_PuPd_UP;
	GPIO_Init(GPIOA,&GPIO_init);
	
	
	
	ADC_DeInit();
	
	ADC_CommonInitTypeDef ADC_Commoninit;
	ADC_Commoninit.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
	ADC_Commoninit.ADC_Mode = ADC_Mode_Independent;
	ADC_Commoninit.ADC_Prescaler = ADC_Prescaler_Div4;
	ADC_Commoninit.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
	ADC_CommonInit(&ADC_Commoninit);
	
	
ADC_InitTypeDef ADC_InitStruct;
ADC_InitStruct.ADC_ContinuousConvMode =DISABLE;
ADC_InitStruct.ADC_DataAlign =ADC_DataAlign_Right;
ADC_InitStruct.ADC_ExternalTrigConvEdge=ADC_ExternalTrigConvEdge_None;
ADC_InitStruct.ADC_NbrOfConversion = 1;
ADC_InitStruct.ADC_Resolution =ADC_Resolution_12b;
ADC_InitStruct.ADC_ScanConvMode =DISABLE;
ADC_Init(ADC1,&ADC_InitStruct);
ADC_Cmd(ADC1,ENABLE);

ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_3Cycles);


}


uint16_t Get_ADC_Value()
{

	//ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_3Cycles);

	ADC_SoftwareStartConv(ADC1);
	
	while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC))
	{}
	
	return ADC_GetConversionValue(ADC1)*3300/4096;

}
uint32_t getAdc(uint8_t times)
{
	uint32_t temp_val=0;
	uint8_t  t;
	
	for(t = 0 ; t<times ;t++ )
	{
		temp_val += Get_ADC_Value();
		delay_ms(5);
		
	}
		return temp_val/times;
}
